// src\store\modules\app.js
import defaultSettings from '@/settings';
import { store } from '@/store';
import { DeviceEnum } from '@/enums';
import { SidebarStatusEnum } from '@/enums';
import { VxeSizeEnum } from '@/enums';
import { SizeEnum } from '@/enums';
import { useDictStoreHook } from './dict';

// 导入 Element Plus 中英文语言包
import zhCn from 'element-plus/dist/locale/zh-cn.mjs';
// import zhTw from 'element-plus/dist/locale/zh-tw.mjs';
import en from 'element-plus/dist/locale/en.mjs';
// import ja from 'element-plus/dist/locale/ja.mjs';
// import ko from 'element-plus/dist/locale/ko.mjs';
// import id from 'element-plus/dist/locale/id.mjs';
// import th from 'element-plus/dist/locale/th.mjs';
// import vi from 'element-plus/dist/locale/vi.mjs';
// import ru from 'element-plus/dist/locale/ru.mjs';
// import de from 'element-plus/dist/locale/de.mjs';
// import es from 'element-plus/dist/locale/es.mjs';
// import fr from 'element-plus/dist/locale/fr.mjs';
// import it from 'element-plus/dist/locale/it.mjs';
// import pt from 'element-plus/dist/locale/pt.mjs';

// 导入 VxeUI 语言包
import { VxeUI } from 'vxe-pc-ui';
import vxeZhCN from 'vxe-pc-ui/lib/language/zh-CN';
// import vxeZhTW from 'vxe-pc-ui/lib/language/zh-TW';
import vxeEnUS from 'vxe-pc-ui/lib/language/en-US';
// import vxeJaJP from 'vxe-pc-ui/lib/language/ja-JP';
// import vxeKoKR from 'vxe-pc-ui/lib/language/ko-KR';
// import vxeIdID from 'vxe-pc-ui/lib/language/id-ID';
// import vxeThTH from 'vxe-pc-ui/lib/language/th-TH';
// import vxeViVN from 'vxe-pc-ui/lib/language/vi-VN';
// import vxeRuRU from 'vxe-pc-ui/lib/language/ru-RU';
// import vxeDeDE from 'vxe-pc-ui/lib/language/de-DE';
// import vxeEsES from 'vxe-pc-ui/lib/language/es-ES';
// import vxeFrFR from 'vxe-pc-ui/lib/language/fr-FR';
// import vxeItIT from 'vxe-pc-ui/lib/language/it-IT';
// import vxePtBR from 'vxe-pc-ui/lib/language/pt-BR';

// 统一的语言代码映射表
const languageMap = {
  'zh-cn': {
    element: zhCn,
    vxe: vxeZhCN,
    vxeCode: 'zh-CN',
  },
  // 'zh-tw': {
  //   element: zhTw,
  //   vxe: vxeZhTW,
  //   vxeCode: 'zh-TW',
  // },
  en: {
    element: en,
    vxe: vxeEnUS,
    vxeCode: 'en-US',
  },
  // ja: {
  //   element: ja,
  //   vxe: vxeJaJP,
  //   vxeCode: 'ja-JP',
  // },
  // ko: {
  //   element: ko,
  //   vxe: vxeKoKR,
  //   vxeCode: 'ko-KR',
  // },
  // id: {
  //   element: id,
  //   vxe: vxeIdID,
  //   vxeCode: 'id-ID',
  // },
  // th: {
  //   element: th,
  //   vxe: vxeThTH,
  //   vxeCode: 'th-TH',
  // },
  // vi: {
  //   element: vi,
  //   vxe: vxeViVN,
  //   vxeCode: 'vi-VN',
  // },
  // ru: {
  //   element: ru,
  //   vxe: vxeRuRU,
  //   vxeCode: 'ru-RU',
  // },
  // de: {
  //   element: de,
  //   vxe: vxeDeDE,
  //   vxeCode: 'de-DE',
  // },
  // es: {
  //   element: es,
  //   vxe: vxeEsES,
  //   vxeCode: 'es-ES',
  // },
  // fr: {
  //   element: fr,
  //   vxe: vxeFrFR,
  //   vxeCode: 'fr-FR',
  // },
  // it: {
  //   element: it,
  //   vxe: vxeItIT,
  //   vxeCode: 'it-IT',
  // },
  // pt: {
  //   element: pt,
  //   vxe: vxePtBR,
  //   vxeCode: 'pt-BR',
  // },
};

for (const key in languageMap) {
  if (Object.hasOwnProperty.call(languageMap, key)) {
    const langConfig = languageMap[key];
    if (langConfig.vxe && langConfig.vxeCode) {
      VxeUI.setI18n(langConfig.vxeCode, langConfig.vxe);
    }
  }
}

// 创建统一的 app 存储对象
const appStorage = useStorage('app', {
  device: DeviceEnum.DESKTOP,
  size: defaultSettings.size,
  vxeSize: defaultSettings.vxeSize,
  language: defaultSettings.language,
  sidebarStatus: SidebarStatusEnum.CLOSED,
  activeTopMenuPath: '',
});

export const useAppStore = defineStore('app', () => {
  // 从 app 对象中解构出各个属性并保持响应式
  const device = computed({
    get: () => appStorage.value.device,
    set: (v) => (appStorage.value.device = v),
  });

  const size = computed({
    get: () => appStorage.value.size,
    set: (v) => (appStorage.value.size = v),
  });

  const vxeSize = computed({
    get: () => appStorage.value.vxeSize,
    set: (v) => (appStorage.value.vxeSize = v),
  });

  const language = computed({
    get: () => appStorage.value.language,
    set: (v) => (appStorage.value.language = v),
  });

  const sidebarStatus = computed({
    get: () => appStorage.value.sidebarStatus,
    set: (v) => (appStorage.value.sidebarStatus = v),
  });

  const activeTopMenuPath = computed({
    get: () => appStorage.value.activeTopMenuPath,
    set: (v) => (appStorage.value.activeTopMenuPath = v),
  });

  // 侧边栏状态（保持响应式）
  const sidebar = reactive({
    opened: computed(() => sidebarStatus.value === SidebarStatusEnum.OPENED),
    withoutAnimation: false,
  });

  // 语言包计算属性
  const locale = computed(() => {
    const currentLangConfig = languageMap[language.value];
    return currentLangConfig ? currentLangConfig.element : zhCn; // 默认中文
  });

  // 切换侧边栏
  function toggleSidebar() {
    sidebarStatus.value = sidebar.opened
      ? SidebarStatusEnum.CLOSED
      : SidebarStatusEnum.OPENED;
    sidebar.withoutAnimation = false;
  }

  // 关闭侧边栏
  function closeSideBar() {
    sidebarStatus.value = SidebarStatusEnum.CLOSED;
    sidebar.withoutAnimation = false;
  }

  // 打开侧边栏
  function openSideBar() {
    sidebarStatus.value = SidebarStatusEnum.OPENED;
    sidebar.withoutAnimation = false;
  }

  // 切换设备
  function toggleDevice(val) {
    device.value = val;
  }

  /**
   * 改变布局大小
   *
   * @param val 布局大小 default | small | large
   */
  function changeSize(val) {
    size.value = val;
    vxeSize.value = convertSize(val);
  }

  // 将element-plus的布局大小，转换为vxe-table的布局大小
  function convertSize(val) {
    if (val === SizeEnum.DEFAULT) {
      return VxeSizeEnum.DEFAULT;
    }
    if (val === SizeEnum.LARGE) {
      return VxeSizeEnum.LARGE;
    }
    if (val === SizeEnum.SMALL) {
      return VxeSizeEnum.SMALL;
    }
  }

  /**
   * 切换语言
   *
   * @param val
   */
  function changeLanguage(val) {
    language.value = val;
    // 切换 VxeUI 语言
    const currentLangConfig = languageMap[val];
    if (currentLangConfig && currentLangConfig.vxeCode) {
      VxeUI.setLanguage(currentLangConfig.vxeCode);
    }
    // 重新载入字典数据
    useDictStoreHook().refreshDictionaryCache();
  }

  /**
   * 混合模式顶部切换
   */
  function activeTopMenu(val) {
    activeTopMenuPath.value = val;
  }

  /**
   * 获取语言包映射表
   */
  function getLanguageMap() {
    return languageMap;
  }

  return {
    device,
    sidebar,
    language,
    locale,
    size,
    vxeSize,
    getLanguageMap,
    activeTopMenu,
    toggleDevice,
    changeSize,
    changeLanguage,
    toggleSidebar,
    closeSideBar,
    openSideBar,
    activeTopMenuPath,
  };
});

/**
 * 用于在组件外部（如在Pinia Store 中）使用 Pinia 提供的 store 实例。
 * 官方文档解释了如何在组件外部使用 Pinia Store：
 * https://pinia.vuejs.org/core-concepts/outside-component-usage.html#using-a-store-outside-of-a-component
 */
export function useAppStoreHook() {
  return useAppStore(store);
}
